/*
 * 创建日期 2005-9-30
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.vanda.report.view;

import java.io.InputStream;
import java.util.HashMap;

import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.vanda.report.Constansts;
import com.vanda.report.ExcelUtils;
import com.vanda.report.ReportException;
import com.vanda.report.ReportInnerData;
import com.vanda.report.ReportUtils;
import com.vanda.report.cfg.Configuration;
import com.vanda.report.element.Element;
/**
 * 显示层开始标签，将之前处理的数据放到Excel表 <br>
 * <p>
 * 
 * @author eagle <br>
 * @version Version 1.00 <br>
 */
public class ViewElement extends Element{
	/** 指定报表模板文件名 */
	private String format_file_name;
	/** 指定报表模板所在sheet名 */
	private String format_sheet;
	
	public void validator() throws ReportException{
		//Validation.checkRequired("ViewElement","format_file_name",format_file_name);
		//Validation.checkRequired("ViewElement","format_sheet",format_sheet);
	}
	
	/**
	 * 标签执行方法，递归执行
	 * 临时变量说明:
	 * temp1:保存生成sheet的名字,HashMap,key-名字,value-名字
	 * @param obj
	 */
	public void execute(Object obj) throws ReportException{
		ReportInnerData reportData = (ReportInnerData)obj;
		
		//取得报表模板sheet
		InputStream is = null;
		HashMap params = reportData.getHmParams();
		try{
			String fileName = (String)params.get("format_file_name");
			if(fileName == null){
				fileName = format_file_name;
				params.put("format_file_name",fileName);
			}
			is = ReportUtils.getMappingStream(fileName,Configuration.modelURI);
			
			HSSFWorkbook workbook = new HSSFWorkbook(is);
			
			HSSFSheet sheet = workbook.getSheet(format_sheet);
			
			reportData.setWorkbook(workbook);
			reportData.setFormatSheet(sheet);

			//清空temp1
			HashMap hmSheet = new HashMap();
			reportData.setTemp1(hmSheet);
			
			super.execute(obj);
			
			//如果生成sheet数大于0
			boolean first = true;
			if(hmSheet.size() > 0){
				//删除多余的sheet,因为removeSheet使用jxl,因此workbook重新生成
				workbook = ExcelUtils.removeSheet(workbook,hmSheet);
				reportData.setWorkbook(workbook);
			
				//设置打开excel时看到的是第一个sheet
				for(int i=0;i<workbook.getNumberOfSheets();i++){
					HSSFSheet tmpSheet = workbook.getSheetAt(i);
					//设置打开excel时看到的是第一个生成的sheet
					if(hmSheet.get(workbook.getSheetName(i)) != null){
						if(first == true){
							tmpSheet.setSelected(true);
							first = false;
							HSSFHeader header = tmpSheet.getHeader();
							
							header.setRight("\n\n\n"+HSSFHeader.page()+"/"+HSSFHeader.numPages());
						}else{
							tmpSheet.setSelected(false);
						}
					}else{
						tmpSheet.setSelected(false);
					}
				}
			}else{
				//如果没有新sheet,则设置状态为empty
				reportData.setStatus(Constansts.ReportRunFlag_Empty);
			}

			
			//清空tmep1
			reportData.setTemp1(null);

		}catch(ReportException e){
			throw e;
		}catch(Exception e){
			throw new ReportException(e);
		}finally{
			try{
				if(is != null)
					is.close();
			}catch(Exception e){}
		}

	}
	
	/**
	 * @return
	 */
	public String getFormat_file_name() {
		return format_file_name;
	}

	/**
	 * @return
	 */
	public String getFormat_sheet() {
		return format_sheet;
	}

	/**
	 * @param string
	 */
	public void setFormat_file_name(String string) {
		format_file_name = string;
	}

	/**
	 * @param string
	 */
	public void setFormat_sheet(String string) {
		format_sheet = string;
	}
	
	public static void main(String[] args){
		//取得报表模板sheet
		InputStream is = null;
		InputStream is1 = null;
		try{
			is = new java.io.FileInputStream("c:/test/excel/report-7-31.xls");
			
			HSSFWorkbook workbook = new HSSFWorkbook(is);
			
			HSSFSheet sheet = workbook.getSheetAt(0);
			
			/*for(int i=24;i<=70;i++){
				HSSFRow row = sheet.getRow(i);
				if(row != null)
				sheet.removeRow(row);
			}*/
			/*HashMap hmSheet = new HashMap();
			hmSheet.put("1","1");
			hmSheet.put("Sheet1","1");
			workbook = ExcelUtils.removeSheet(workbook,hmSheet);*/
			workbook.removeSheetAt(0);
			
			workbook.write(new java.io.FileOutputStream("c:/test/excel/test_out.xls"));
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
